package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-11 12:05
 **/

@DubboService
public class RecommendUserImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    //根据touserId查询缘分值最高的一项推荐数据
    @Override
    public RecommendUser findwithMaxScore(Long touserId) {
        //1.构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(touserId);
        //2.根据Criteria构建Query
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score"))) //排序
                .limit(1);

        //3.调用find或者findOne查询
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        if(user == null){
            //构建假数据
            user = new RecommendUser();
            user.setUserId(1L);//推荐
            user.setToUserId(touserId);
            user.setScore(95d);

        }
        return user;
    }


    //根据toUSerId分页查询分页数据
    @Override
    public List<RecommendUser> findRecommtUsetList(Long toUserId, Integer page, Integer pageSize) {
        //1.构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2.根据Criteria构建Query
        Query query = Query.query(criteria);
        //3.查询总数
        long count = mongoTemplate.count(query,RecommendUser.class);
        System.out.println("总条数="+count);
        //4.向query对象中设置分页参数
        query.with(Sort.by(Sort.Order.desc("score")))
                .limit(pageSize)
                .skip((page-1)*pageSize);

        //5.调用MongoTemplate的find方法，查询数据列表
       return mongoTemplate.find(query,RecommendUser.class);
    }

    //根据用户id和toUserId查询推荐数据
    @Override
    public RecommendUser queryByUserId(Long userId, Long toUserId) {
        //select * from tab where userId = ? AND toUserId = ?
        //1.构建Criteria并设置查询条件
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("toUserId").is(toUserId);
        //2.通过Query构建Query
        Query query = Query.query(criteria);
        //3.通过MongoTemplate的find或者findOne查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if(recommendUser == null){
            //新用户，设置默认值
            RecommendUser user = new RecommendUser();
            user.setUserId(userId);
            user.setToUserId(toUserId);
            user.setScore(95d);
        }
        return recommendUser;
    }


    //探花推荐好友
    @Override
    public List<RecommendUser> findCardsUser(Long userId ,int counts) {
        //查询不喜欢 的用户id
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria);
        List<UserLike> userLikes = mongoTemplate.find(query ,UserLike.class);
        List<Long> ids = CollUtil.getFieldValues(userLikes ,"likeUserId" ,Long.class);
        // 构造查询条件（查询推荐用户的条件）  toUserId用户id    nin不包含
        Criteria criteria1 = Criteria.where("toUserId").is(userId)
                .and("userId").nin(ids);
        // 聚合查询
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria1),//指定查询条件
                Aggregation.sample(counts));
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        return results.getMappedResults();
    }
}
